class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr)
            return nullptr;
        ListNode* prev = head;
        ListNode* cur = head->next;
        ListNode* nex = cur;
        if (cur)
            nex = cur->next;
        prev->next = nullptr;
        while (cur)
        {
            cur->next = prev;
            prev = cur;
            cur = nex;
            if (nex)
                nex = nex->next;
        }
        return prev;
    }
};